package com.winit.openapi.apiadapter.controller.validator;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.vo.OutboundOrderVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderMerchandiseVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

public class OrderAdapteValidator {

    /**
     * 出库单列表查询校验.
     * 
     * @param json
     */
    public static void validInputPara(JSONObject json) {

        if (json.containsKey("warehouseId")) {// 订单号
            ValidateUtil.validMaxLength(json, "warehouseId", 20);
        }
        if (json.containsKey("outboundOrderNum")) {// 订单号
            ValidateUtil.validMaxLength(json, "outboundOrderNum", 64);
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 40);
        }
        if (json.containsKey("status")) {// 订单状态
            ValidateUtil.validMaxLength(json, "status", 30);
        }
        if (json.containsKey("receiverName")) {// 收件人姓名
            ValidateUtil.validMaxLength(json, "receiverName", 60);
        }
        if (json.containsKey("dispatchWhCode")) {// 发货仓编号
            ValidateUtil.validMaxLength(json, "dispatchWhCode", 40);
        }
        if (json.containsKey("shareOrderType")) {// 订单类型
            ValidateUtil.validMaxLength(json, "shareOrderType", 1);
        }
        if (json.containsKey("trackingNo")) {// 快递单号
            ValidateUtil.validMaxLength(json, "trackingNo", 32);
        }
        if (json.containsKey("productValue")) {// 商品编码
            ValidateUtil.validMaxLength(json, "productValue", 255);
        }
        if (json.containsKey("productName")) {// 商品名称
            ValidateUtil.validMaxLength(json, "productName", 255);
        }
        if (json.containsKey("bookingOperator")) {// 下单人
            ValidateUtil.validMaxLength(json, "bookingOperator", 60);
        }
    }

    public static void validateQueryOrderListPara(JSONObject json) {
        ValidateUtil.validMaxLength(json, "outboundOrderNum", 64, false);
        ValidateUtil.validMaxLength(json, "warehouseId", 10, false);
        ValidateUtil.validMaxLength(json, "sellerOrderNo", 64, false);
        ValidateUtil.validMaxLength(json, "trackingNo", 64, false);
        ValidateUtil.validMaxLength(json, "receiverName", 100, false);
        ValidateUtil.validMaxLength(json, "bookingOperator", 100, false);
        ValidateUtil.validMaxLength(json, "productValue", 128, false);
        ValidateUtil.validMaxLength(json, "productName", 128, false);
        ValidateUtil.validMaxLength(json, "productSku", 128, false);
        ValidateUtil.validMaxLength(json, "shareOrderType", 1, false);
        ValidateUtil.validByRegex(json, "shareOrderType", "[0123]", false);

        if (json.containsKey("dateOrderedStartDate") && json.containsKey("dateOrderedEndDate")) {
            ValidateUtil.validDateString(json, "dateOrderedStartDate", "yyyy-MM-dd");
            ValidateUtil.validDateString(json, "dateOrderedEndDate", "yyyy-MM-dd");
            ValidateUtil.validDateBefore(json, "dateOrderedStartDate", "dateOrderedEndDate");
        } else if (json.containsKey("dateOrderedStartDate") || json.containsKey("dateOrderedEndDate")) {

            throw new ApiException(ErrorCode.FIELD_NOT_NULL,
                json.containsKey("dateOrderedStartDate") ? "dateOrderedEndDate" : "dateOrderedStartDate");
        }

        if (json.containsKey("pageSize")) {
            ValidateUtil.validInteger(json, "pageSize", true);
            ValidateUtil.validNumberBiggerThanZero(json, "pageSize");

        }
        if (json.containsKey("pageNum")) {
            ValidateUtil.validInteger(json, "pageNum", true);
            ValidateUtil.validNumberBiggerThanZero(json, "pageNum");
        }

    }

    /**
     * 校验订单是否包含退货商品
     * 
     * @param order
     * @return
     */
    public static boolean validateIsRetrunProduct(OutboundOrderVo orderVo) {
        List<OutboundOrderMerchandiseVo> merchandiseInfoList = orderVo.getMerchandiseInfoList();

        for (OutboundOrderMerchandiseVo merchandiseVo : merchandiseInfoList) {
            if (merchandiseVo.getMerchandiseCode().contains("(return)")) {
                return false;

            }
        }
        return true;
    }

    /**
     * 验证 OutboundOrder
     * 
     * @param json
     */
    public static void validCreateOutboundOrder(JSONObject json) {
        ValidateUtil.valid(json, "warehouseID", true, true, 10);
        ValidateUtil.valid(json, "repeatable", true, false, 1);
        ValidateUtil.validByRegex(json, "repeatable", "[YN]", true);

        ValidateUtil.valid(json, "deliveryWayID", true, true, 20);
        ValidateUtil.valid(json, "insuranceTypeID", false, true, 10);
        ValidateUtil.valid(json, "recipientName", true, false, 255);
        ValidateUtil.valid(json, "phoneNum", false, false, 20);
        ValidateUtil.valid(json, "zipCode", true, false, 10);
        ValidateUtil.valid(json, "emailAddress", false, false, 50);
        ValidateUtil.valid(json, "state", true, false, 50);
        ValidateUtil.valid(json, "region", false, false, 50);
        ValidateUtil.valid(json, "city", true, false, 50);
        ValidateUtil.valid(json, "address1", true, false, 100);
        ValidateUtil.valid(json, "address2", false, false, 100);

        if (json.containsKey("isShareOrder") && "Y".equals(json.getString("isShareOrder"))) {
            ValidateUtil.valid(json, "fromBpartnerId", true, true, 10);
        }

        // 添加增值服务入参校验
        if (json.containsKey("VASList")) {
            JSONArray vasArr = json.getJSONArray("VASList");
            if (vasArr == null || vasArr.size() == 0) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "VASList");
            }
            for (int i = 0; i < vasArr.size(); i++) {
                JSONObject vasServiceJson = vasArr.getJSONObject(i);
                ValidateUtil.valid(vasServiceJson, "serviceCode", true, false, 32);
                JSONArray attrArray = vasServiceJson.getJSONArray("attrList");
                if (null == attrArray || attrArray.size() == 0) {
                    //throw new ApiException(ErrorCode.FIELD_NOT_NULL, "attrList");
                    continue;
                }
                for (int j = 0; j < attrArray.size(); j++) {
                    JSONObject attrJson = attrArray.getJSONObject(j);
                    
                    ValidateUtil.validNotNull(attrJson, "attrCode");
                    ValidateUtil.validMaxLength(attrJson, "attrCode", 128, true);
                    ValidateUtil.validNotNull(attrJson, "attrValue");
                    ValidateUtil.validMaxLength(attrJson, "attrValue", 128, true);
                    ValidateUtil.validBigDecimal(attrJson,"attrValue");
                    //校验属性值只支持最大6位（包含）小数
                    ValidateUtil.validatePrecision(attrJson, "attrValue", 10, 6);
                }
            }
        }
        if (json.containsKey("productList")) {
            JSONArray arr = json.getJSONArray("productList");
            for (int i = 0; i < arr.size(); i++) {
                JSONObject productJson = arr.getJSONObject(i);
                ValidateUtil.valid(productJson, "productCode", true, false, 100);
                ValidateUtil.valid(productJson, "productNum", true, true, 10);
                int productNum = productJson.getInteger("productNum");
                // ValidateUtil(productJson.getInteger("productNum"),
                // "productNum", 1);
                if (productNum < 1) {
                    throw new ApiException(ErrorCode.FIELD_MUST_BE_BIGGER_THAN_ZERO);
                }

                ValidateUtil.valid(productJson, "specification", false, false, 255);
                ValidateUtil.valid(productJson, "eBayTransactionID", false, false, 30);
                ValidateUtil.valid(productJson, "eBayItemID", false, false, 30);
                ValidateUtil.valid(productJson, "eBaySellerID", false, false, 50);
                ValidateUtil.valid(productJson, "eBayBuyerID", false, false, 50);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "productList");
        }
    }

}
